package cn.hftoday.org;

import java.util.*;
import org.apache.log4j.*;
import cn.hftoday.db.*;
import cn.hftoday.core.*;
import cn.hftoday.util.*;

/**
 * role
 * @author lane.cn@gmail.com
 *
 */
public class Role extends PartyBase {
	private static final Logger LOG = Logger.getLogger(Role.class);
	
	public static final int ROLE_TYPE_COMPANY = 1;
	public static final int ROLE_TYPE_APPLICATION = 2;
	
	private String description;
	private Calendar beginTime = CalendarParser.MIN_TIME;
	private Calendar endTime = CalendarParser.MAX_TIME;
	private int roleType;
	private String companyId;
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public Calendar getBeginTime() {
		return beginTime;
	}
	
	public void setBeginTime(Calendar beginTime) {
		this.beginTime = beginTime;
	}
	
	public Calendar getEndTime() {
		return endTime;
	}
	
	public void setEndTime(Calendar endTime) {
		this.endTime = endTime;
	}
	
	public int getRoleType() {
		return roleType;
	}
	
	public void setRoleType(int roleType) {
		this.roleType = roleType;
	}

	public Company getCompany() {
		return (Company)Mapper.getInstance().queryForObject("SelectCompanyById", companyId);
	}
	
	public void setCompany(Company company) {
		if (company == null) {
			companyId = null;
		}
		else {
			companyId = company.getId();
		}
	}
	
	public String getPartyType() {
		return "role";
	}
	
	private static void _select(Object object, RecordSet rs, int i) {
		Role o = (Role)object;
		o.id = rs.getItem(i, "f_id");
		o.name = rs.getItem(i, "f_name");
		o.hidden = rs.getItem(i, "f_hidden").equals("1");
		o.createTime = CalendarParser.toCalendar(rs.getItem(i, "f_createtime"));
		o.updateTime = CalendarParser.toCalendar(rs.getItem(i, "f_updatetime"));
		o.description = rs.getItem(i, "f_description");
		o.beginTime = CalendarParser.toCalendar(rs.getItem(i, "f_begintime"));
		o.endTime = CalendarParser.toCalendar(rs.getItem(i, "f_endtime"));
		o.roleType = Integer.parseInt(rs.getItem(i, "f_roletype"));
		o.companyId = rs.getItem(i, "f_company");
	}
	
	private static String _insert(Object object, String sql) {
		Role o = (Role)object;
		return StringParser.formatSql(sql,
			o.id,
			o.name,
			o.hidden ? "1" : "0",
			CalendarParser.toString(o.createTime),
			CalendarParser.toString(o.updateTime),
			o.description,
			CalendarParser.toString(o.beginTime),
			CalendarParser.toString(o.endTime),
			"" + o.roleType,
			o.companyId
		);
	}
	
	private static String _update(Object object, String sql) {
		Role o = (Role)object;
		return StringParser.formatSql(sql,
			o.id,
			o.name,
			o.hidden ? "1" : "0",
			CalendarParser.toString(o.createTime),
			CalendarParser.toString(o.updateTime),
			o.description,
			CalendarParser.toString(o.beginTime),
			CalendarParser.toString(o.endTime),
			"" + o.roleType,
			o.companyId
		);
	}
	
	private static String _delete(Object object, String sql) {
		Role o = (Role)object;
		return StringParser.formatSql(sql, o.id);
	}
}
